using System.Collections;
using UnityEngine;
using Utils;

public class Game : MonoBehaviour
{
    public Camera mainCamera;
    public GameObject gameCoreGO;

    private GameController gameController;
    private MapController mapController;
    private PlayerController playerController;
    private ArchiveController archiveController;
    private WeaponController weaponController;
    private PanelController panelController;
    private PlayerDataController playerDataController;

    private void Init()
    {
        Time.timeScale = 1;
        LoadGame();
    }

    private void Awake()
    {
        Init();
    }

    void Start()
    {
    }

    void Update()
    {
        Pause();
    }

    private void InitGameController()
    {
        gameController = GameController.Instance();
        gameController.GameHeart = this;
        gameController.gameUIRoot = Instantiate(UITool.LoadPrefab(PrefabRoot.GameUI.Path)).transform; //GameUI
        gameController.panelCtr = new PanelController();

        gameCoreGO = UITool.InstantiatePrefab(PrefabRoot.GameCore.Path, PrefabRoot.GameCore.Name); //GameCore
        gameController.gameCore = gameCoreGO;
        mapController = UITool.GetChildGameObject("Map", gameCoreGO).GetComponent<MapController>();
        playerController = UITool.GetChildGameObject("Player", gameCoreGO).GetComponent<PlayerController>();
        weaponController = UITool.GetChildGameObject("Player", gameCoreGO).GetComponent<WeaponController>();

        gameController.mapCtr = mapController;
        gameController.playerCtr = playerController;
        gameController.weaponCtr = weaponController;
        gameController.ObjectCtr = new ObjectController();
        gameController.archiveCtr = new ArchiveController();
        gameController.playerDataCtr = new PlayerDataController();
        gameController.audioCtr = new AudioController
        {
            audioRoot = new AudioRoot(),
            bgM = gameController.gameCore.GetComponent<AudioSource>(),
        };
        gameController.audioCtr.bgM.clip = gameController.audioCtr.audioRoot.AudioClipMap["Elecrystal Sound Team"];
        gameController.audioCtr.bgM.volume = gameController.playerDataCtr.setting.BgMValue * 0.01f;
        gameController.audioCtr.bgM.Play();
        if (!gameController.playerDataCtr.setting.BgMState)
        {
            gameController.audioCtr.bgM.Pause();
        }
        gameController.FSM = new FSMController
        {
            CurGameState = GameState.Login,
            CurPlayerState = PlayerState.NotFight,
        };
        mainCamera = UITool.GetComponent<Camera>("MainCamera", gameCoreGO);
    }

    /// <summary>
    /// 加载游戏(初始化)
    /// </summary>
    private void LoadGame()
    {
        InitGameController();
        // if (UITool.GetChildGameObject("LoginPanel", gameController.gameUIRoot.gameObject))
        // {
        //     return;
        // }
        gameController.panelCtr.LoadLoginPanel();
    }

    private void Pause()
    {
        if (gameController.FSM.CurGameState == GameState.Gaming && Input.GetKeyDown(KeyCode.Escape) &&
            Time.timeScale != 0)
        {
            gameController.panelCtr.LoadPausePanel();
        }
    }

    public void ExitGame()
    {
        DestroyGO();
        Init();
    }

    public void Restart()
    {
        DestroyGO();
        Time.timeScale = 1;
        InitGameController();
        // GameController.Instance().archiveCtr.LoadArchive();
        StartCoroutine(RestartGame());
    }

    private IEnumerator RestartGame()
    {
        gameController.panelCtr.LoadLoadingPanel();
        yield return new WaitForSeconds(2);
        gameController.archiveCtr.LoadArchive();
    }

    public void DestroyGO()
    {
        gameController.gameCore.gameObject.SetActive(false);
        gameController.gameUIRoot.gameObject.SetActive(false);
        Destroy(gameController.gameCore);
        Destroy(gameController.gameUIRoot.gameObject);
        Destroy(gameController.smallMap);
        gameController.CleanAllController();
        gameController = null;
        Resources.UnloadUnusedAssets();
    }
}